package com.mde.xmlreader;

import java.util.ArrayList;
import java.util.List;

public class Node {
	
	private String openingTag;
	
	private String closingTag;
	
	private List<String> innerTags;
	
	public Node(String openingTag, String closinTag, List<String> innerTags) {
		this.openingTag = openingTag;
		this.closingTag = closinTag;
		this.innerTags = new ArrayList<String>(innerTags);
	}
	
	public boolean has(String pattern) {
		for (String st : innerTags) {
			if (st.contains(pattern))
				return true;
		}
		return false;
	}
	
	public double getLatitude() {
		StringBuilder str = new StringBuilder();
		int pos = openingTag.indexOf("lat=\"") + 5;
		while (openingTag.charAt(pos) != '\"') 
			str.append(openingTag.charAt(pos++));
		
		double r = 0;
		
		try {
			r = Double.parseDouble(str.toString());
			return r;
		} catch (Exception e) {
			System.out.println("Trying to parse { " + str.toString() + " }");
			System.out.println(this.toString());
			return Double.parseDouble(str.toString());
		}
		
		 
	}
	
	public double getLongitude() {
		StringBuilder str = new StringBuilder();
		int pos = openingTag.indexOf("lon=\"") + 5;
		while (openingTag.charAt(pos) != '\"') 
			str.append(openingTag.charAt(pos++));
		
		double r = 0;
		
		try {
			r = Double.parseDouble(str.toString());
			return r;
		} catch (Exception e) {
			System.out.println("Trying to parse " + str.toString());
			return Double.parseDouble(str.toString());
		}
	}
	
	@Override
	public String toString() {
		StringBuilder res = new StringBuilder(openingTag + "\n");
		for (String st : innerTags) {
			res.append(st + "\n");
		}
		res.append(closingTag + "\n");
		return res.toString();
	}

}
